using Avalonia;
using Avalonia.Controls;
using Avalonia.Media;
using FaceCat;
using System;
using static System.Net.Mime.MediaTypeNames;

namespace facecat_avalonia.Views;

/// <summary>
/// 自定义视图
/// </summary>
public partial class MainView : UserControl
{
    /// <summary>
    /// 构造函数
    /// </summary>
    public MainView()
    {
        InitializeComponent();
        onLoad();
    }

    public Canvas m_canvas;

    public TextBox m_textBox;

    /// <summary>
    /// 虚拟输入框
    /// </summary>
    public FCView m_editingTextBox;

    /// <summary>
    /// 是否双击
    /// </summary>
    private bool m_isDoubleClick;

    /// <summary>
    /// 公共方法库
    /// </summary>
    public FCNative m_native;

    /// <summary>
    /// 设备对接
    /// </summary>
    public AvaloniaHost m_host;

    /// <summary>
    /// 配置解析
    /// </summary>
    public MainFrame m_xml;

    /// <summary>
    /// 鼠标滚轮事件
    /// </summary>
    /// <param name="sender">调用者</param>
    /// <param name="e">参数</param>
    private void MainView_PointerWheelChanged(object? sender, Avalonia.Input.PointerWheelEventArgs e)
    {
        if (m_host.allowOperate())
        {
            FCTouchInfo newTouchInfo = new FCTouchInfo();
            newTouchInfo.m_delta = (int)e.Delta.Y;
            m_native.onMouseWheel(newTouchInfo);
        }
    }

    public bool m_rightClick;

    /// <summary>
    /// 鼠标松开事件
    /// </summary>
    /// <param name="sender">调用者</param>
    /// <param name="e">参数</param>
    private void MainView_PointerReleased(object? sender, Avalonia.Input.PointerReleasedEventArgs e)
    {
        m_host.m_touchPoint = new FCPoint((int)e.GetPosition(this).X, (int)e.GetPosition(this).Y);
        if (m_host.allowOperate())
        {
            FCTouchInfo newTouchInfo = new FCTouchInfo();
            if (m_rightClick)
            {
                newTouchInfo.m_secondTouch = true;
            }
            else
            {
                newTouchInfo.m_firstTouch = true;
            }
            newTouchInfo.m_buttons = 1;
            newTouchInfo.m_clicks = m_isDoubleClick ? 2 : 1;
            m_native.onMouseUp(newTouchInfo);
        }
    }

    /// <summary>
    /// 鼠标移动事件
    /// </summary>
    /// <param name="sender">调用者</param>
    /// <param name="e">参数</param>
    private void MainView_PointerMoved(object? sender, Avalonia.Input.PointerEventArgs e)
    {
        m_host.m_touchPoint = new FCPoint((int)e.GetPosition(this).X, (int)e.GetPosition(this).Y);
        if (m_host.allowOperate())
        {
            if (m_native.m_touchDownView != null)
            {
                FCTouchInfo newTouchInfo = new FCTouchInfo();
                newTouchInfo.m_firstTouch = true;
                newTouchInfo.m_buttons = 1;
                newTouchInfo.m_clicks = 1;
                m_native.onMouseMove(newTouchInfo);
            }
            else
            {
                FCTouchInfo newTouchInfo = new FCTouchInfo();
                m_native.onMouseMove(newTouchInfo);
            }
        }
    }

    /// <summary>
    /// 鼠标按下事件
    /// </summary>
    /// <param name="sender">调用者</param>
    /// <param name="e">参数</param>
    private void MainView_PointerPressed(object? sender, Avalonia.Input.PointerPressedEventArgs e)
    {
        m_host.m_touchPoint = new FCPoint((int)e.GetPosition(this).X, (int)e.GetPosition(this).Y);
        if (m_host.allowOperate())
        {
            FCTouchInfo newTouchInfo = new FCTouchInfo();
            m_rightClick = e.GetCurrentPoint(this).Properties.IsRightButtonPressed;
            if (m_rightClick)
            {
                newTouchInfo.m_secondTouch = true;
            }
            else
            {
                newTouchInfo.m_firstTouch = true;
            }
            newTouchInfo.m_buttons = 1;
            newTouchInfo.m_clicks = e.ClickCount;
            m_isDoubleClick = e.ClickCount > 1;
            FCView lastFocusedView = m_native.getFocusedView();
            m_native.onMouseDown(newTouchInfo);
            FCView focusedView = m_native.getFocusedView();
            if (focusedView is FCTextBox)
            {
                showTextBox(focusedView, lastFocusedView);
            }
            else
            {
                hideTextBox(focusedView);
            }
        }
    }

    /// <summary>
    /// 显示文本框
    /// </summary>
    /// <param name="view"></param>
    /// <param name="lastView"></param>
    public void showTextBox(FCView view, FCView lastView)
    {
        if (m_textBox != null && m_editingTextBox != null)
        {
            if (lastView != null && lastView != view && lastView is FCTextBox)
            {
                lastView.setText(m_textBox.Text);
                lastView.invalidate();
            }
        }
        int width = (int)Width, height = (int)Height;
        m_editingTextBox = view;
        m_canvas.Width = width;
        m_canvas.Height = height;
        
        int clX = m_native.clientX(view);
        int clY = m_native.clientY(view);
        double scaleFactorX = 0, scaleFactorY = 0;
        m_native.getPaint().getScaleFactor(ref scaleFactorX, ref scaleFactorY);
        Canvas.SetLeft(m_textBox, clX * scaleFactorX);
        Canvas.SetTop(m_textBox, clY * scaleFactorY);
        m_textBox.Width = view.getWidth() * scaleFactorX;
        m_textBox.Height = view.getHeight() * scaleFactorY;
        m_textBox.Text = view.getText();
        FCFont font = view.getFont();
        String fontFamily = font.m_fontFamily;
        if (fontFamily == "Default"){
            fontFamily = MyColor.getSystemFont();
        }
        float fontSize = font.m_fontSize;
        if (scaleFactorX != 1 || scaleFactorY != 1)
        {
            fontSize = (float)(Math.Min(scaleFactorX, scaleFactorY) * fontSize);
        }
        m_textBox.FontSize = fontSize;
        m_textBox.FontFamily = fontFamily;
        m_textBox.IsVisible = true;
        m_textBox.Focus();
    }

    /// <summary>
    /// 隐藏文本框
    /// </summary>
    /// <param name="view"></param>
    public void hideTextBox(FCView view)
    {
        if (m_textBox != null && m_editingTextBox != null)
        {
            m_editingTextBox.setText(m_textBox.Text);
            m_textBox.IsVisible = false;
            m_editingTextBox = null;
            m_native.invalidate();
        }
    }

    /// <summary>
    /// 绘图方法
    /// </summary>
    /// <param name="context">上下文</param>
    public override void Render(DrawingContext context)
    {
        base.Render(context);
        int width = (int)Width, height = (int)Height;
        FCSize oldSize = m_native.getSize();
        if (width > 0 && height > 0)
        {
            if (oldSize.cx != width || oldSize.cy != height)
            {
                m_native.setSize(new FCSize(width, height));
                m_native.update();
            }
        }
        AvaloniaPaint aPaint = m_native.getPaint() as AvaloniaPaint;
        aPaint.m_context = context;
        FCRect clipRect = new FCRect(0, 0, m_native.getSize().cx, m_native.getSize().cy);
        aPaint.beginPaint(0, clipRect, clipRect);
        m_native.onPaint(clipRect);
        aPaint.endPaint();
    }

    /// <summary>
    /// 加载视图
    /// </summary>
    public void onLoad()
    {
        m_canvas = new Canvas();
        m_canvas.Width = this.Width;
        m_canvas.Height = this.Height;
        m_textBox = new TextBox();
        Canvas.SetLeft(m_textBox, 0);
        Canvas.SetTop(m_textBox, 0);
        m_canvas.Children.Add(m_textBox);
        this.Content = m_canvas;
        m_textBox.IsVisible = false;
        m_xml = new MainFrame();
        m_xml.createNative();
        m_native = m_xml.getNative();
        m_native.setPaint(new AvaloniaPaint());
        m_host = new AvaloniaHost();
        m_host.setView(this);
        m_native.setHost(m_host);
        m_host.setNative(m_native);
        m_native.setSize(new FCSize(800, 700));
        string xml = @"<?xml version=""1.0"" encoding=""utf-8"" ?>
        <html xmlns=""facecat"">
        <body>
            <div bordercolor=""none"" name=""divInner"" dock=""fill"">
                <div type=""tab"" dock=""fill"" selectedindex=""0"" backcolor=""none"" bordercolor=""none""
                name=""tabFunc"">
                    <div type=""tabpage"" text=""主界面"" name=""divMain"" backcolor=""none"" bordercolor=""none"">
                    <div type=""splitlayout"" layoutstyle=""lefttoright"" bordercolor=""none"" dock=""fill""
                            size=""610,610"" candragsplitter=""true"" splitterposition=""610,1"" name=""divCodingRight2"">
                            <div type=""custom"" cid=""securitylist"" name=""gridStocks"" headerheight=""30"" dock=""fill""
                                gridlinecolor=""none"" bordercolor=""none"" showvscrollbar=""true"" showhscrollbar=""true""
                                allowpreviewsevent=""true"" allowdragscroll=""true"">
                                <tr>
                                    <th name=""colP0"" text=""序"" width=""40"" allowdrag=""true"" allowresize=""true"" backcolor=""-200000000193""
                                        bordercolor=""-200000000108"" textcolor=""-200000000100"" />
                                    <th name=""colP1"" text=""代码"" width=""70"" allowdrag=""true"" allowresize=""true"" backcolor=""-200000000193""
                                        bordercolor=""-200000000108"" textcolor=""-200000000100"" />
                                    <th name=""colP2"" text=""名称"" width=""70"" allowdrag=""true"" allowresize=""true"" backcolor=""-200000000193""
                                        bordercolor=""-200000000108"" textcolor=""-200000000100"" />
                                    <th name=""colP3"" text=""现价"" width=""60"" allowdrag=""true"" allowresize=""true"" backcolor=""-200000000193""
                                        bordercolor=""-200000000108"" textcolor=""-200000000100"" cellalign=""right""/>
                                    <th name=""colP4"" text=""涨幅"" width=""60"" allowdrag=""true"" allowresize=""true"" backcolor=""-200000000193""
                                        bordercolor=""-200000000108"" textcolor=""-200000000100"" cellalign=""right""/>
                                    <th name=""colP5"" text=""涨跌"" width=""60"" allowdrag=""true"" allowresize=""true"" backcolor=""-200000000193""
                                        bordercolor=""-200000000108"" textcolor=""-200000000100"" cellalign=""right""/>
                                    <th name=""colP9"" text=""总量"" width=""60"" allowdrag=""true"" allowresize=""true"" backcolor=""-200000000193""
                                        bordercolor=""-200000000108"" textcolor=""-200000000100"" cellalign=""right""/>
                                    <th name=""colP10"" text=""总额"" width=""60"" allowdrag=""true"" allowresize=""true"" backcolor=""-200000000193""
                                        bordercolor=""-200000000108"" textcolor=""-200000000100"" cellalign=""right""/>
                                    <th name=""colP11"" text=""量比"" width=""60"" allowdrag=""true"" allowresize=""true"" backcolor=""-200000000193""
                                        bordercolor=""-200000000108"" textcolor=""-200000000100"" cellalign=""right""/>
                                    <th name=""colP12"" text=""PE动"" width=""60"" allowdrag=""true"" allowresize=""true"" backcolor=""-200000000193""
                                        bordercolor=""-200000000108"" textcolor=""-200000000100"" cellalign=""right""/>
                                    <th name=""colP13"" text=""振幅"" width=""60"" allowdrag=""true"" allowresize=""true"" backcolor=""-200000000193""
                                        bordercolor=""-200000000108"" textcolor=""-200000000100"" cellalign=""right""/>
                                    <th name=""colP14"" text=""换手率"" width=""60"" allowdrag=""true"" allowresize=""true"" backcolor=""-200000000193""
                                        bordercolor=""-200000000108"" textcolor=""-200000000100"" cellalign=""right""/>
                                    <th name=""colP15"" text=""总市值"" width=""60"" allowdrag=""true"" allowresize=""true"" backcolor=""-200000000193""
                                        bordercolor=""-200000000108"" textcolor=""-200000000100"" cellalign=""right""/>
                                    <th name=""colP16"" text=""流值"" width=""60"" allowdrag=""true"" allowresize=""true"" backcolor=""-200000000193""
                                        bordercolor=""-200000000108"" textcolor=""-200000000100"" cellalign=""right""/>
                                    <th name=""colP17"" text=""行业分类板块"" width=""80"" allowdrag=""true"" allowresize=""true"" backcolor=""-200000000193""
                                        bordercolor=""-200000000108"" textcolor=""-200000000100"" cellalign=""center""/>
                                    <th name=""colP18"" text=""涨停价"" width=""60"" allowdrag=""true"" allowresize=""true"" backcolor=""-200000000193""
                                        bordercolor=""-200000000108"" textcolor=""-200000000100"" cellalign=""right""/>
                                    <th name=""colP19"" text=""跌停价"" width=""60"" allowdrag=""true"" allowresize=""true"" backcolor=""-200000000193""
                                        bordercolor=""-200000000108"" textcolor=""-200000000100"" cellalign=""right""/>
                                    <th name=""colP20"" text=""金比"" width=""60"" allowdrag=""true"" allowresize=""true"" backcolor=""-200000000193""
                                        bordercolor=""-200000000108"" textcolor=""-200000000100"" cellalign=""right""/>
                                    <th name=""colP21"" text=""涨跌比"" width=""60"" allowdrag=""true"" allowresize=""true"" backcolor=""-200000000193""
                                        bordercolor=""-200000000108"" textcolor=""-200000000100"" cellalign=""right""/>
                                    <th name=""colP22"" text=""涨速"" width=""60"" allowdrag=""true"" allowresize=""true"" backcolor=""-200000000193""
                                        bordercolor=""-200000000108"" textcolor=""-200000000100"" cellalign=""right""/>
                                    <th name=""colP23"" text=""净资产收益率"" width=""100"" allowdrag=""true"" allowresize=""true"" backcolor=""-200000000193""
                                        bordercolor=""-200000000108"" textcolor=""-200000000100"" cellalign=""right""/>
                                    <th name=""colP24"" text=""自设指标"" width=""80"" allowdrag=""true"" allowresize=""true"" backcolor=""-200000000193""
                                        bordercolor=""-200000000108"" textcolor=""-200000000100"" cellalign=""right""/>
                                </tr>
                            </div>
                            <div type=""splitlayout"" layoutstyle=""toptobottom"" bordercolor=""none"" dock=""fill""
                                size=""400,400"" candragsplitter=""true"" splitterposition=""400,1"">
                                <div type=""splitlayout"" layoutstyle=""righttoleft"" bordercolor=""none"" dock=""fill""
                                    size=""400,400"" candragsplitter=""true"" splitterposition=""200,1"">
                                    <div type=""custom"" cid=""latestdiv"" name=""divLatest"" />
                                    <div type=""custom"" cid=""mychart"" name=""mainChart1"" cycle=""0"" />
                                </div>
                                <div type=""splitlayout"" layoutstyle=""lefttoright"" bordercolor=""none"" dock=""fill""
                                    size=""400,400"" candragsplitter=""true"" splitmode=""percentsize"" splitterposition=""200,1"">
                                    <div type=""custom"" cid=""mychart"" name=""mainChart2"" cycle=""5"" />
                                    <div type=""custom"" cid=""mychart"" name=""mainChart3"" cycle=""1440"" />
                                </div>
                            </div>
                        </div>
                        </div>
                        <div type=""tabpage"" text=""多K线"" name=""divMulti"" backcolor=""none"" bordercolor=""none"">
                            <div type=""splitlayout"" layoutstyle=""lefttoright"" backcolor=""none"" bordercolor=""none""
                                dock=""fill"" size=""400,400"" candragsplitter=""true"" splitmode=""percentsize"" splitterposition=""133,1"">
                                <div type=""splitlayout"" layoutstyle=""toptobottom"" backcolor=""none"" bordercolor=""none""
                                    dock=""fill"" size=""400,400"" candragsplitter=""true"" splitmode=""percentsize"" splitterposition=""267,1"">
                                    <div type=""splitlayout"" layoutstyle=""toptobottom"" backcolor=""none"" bordercolor=""none""
                                        dock=""fill"" size=""400,400"" candragsplitter=""true"" splitmode=""percentsize"" splitterposition=""200,1"">
                                        <div type=""custom"" cid=""mychart"" name=""chart1"" cycle=""1"" nativerefresh=""true"" candledivpercent=""1""
                                            voldivpercent=""0"" />
                                        <div type=""custom"" cid=""mychart"" name=""chart2"" cycle=""5"" nativerefresh=""true"" candledivpercent=""1""
                                            voldivpercent=""0"" />
                                    </div>
                                    <div type=""custom"" cid=""mychart"" name=""chart3"" cycle=""10"" nativerefresh=""true"" candledivpercent=""1""
                                        voldivpercent=""0"" />
                                </div>
                                <div type=""splitlayout"" layoutstyle=""lefttoright"" backcolor=""none"" bordercolor=""none""
                                    dock=""fill"" size=""400,400"" candragsplitter=""true"" splitmode=""percentsize"" splitterposition=""200,1"">
                                    <div type=""splitlayout"" layoutstyle=""toptobottom"" backcolor=""none"" bordercolor=""none""
                                        dock=""fill"" size=""400,400"" candragsplitter=""true"" splitmode=""percentsize"" splitterposition=""267,1"">
                                        <div type=""splitlayout"" layoutstyle=""toptobottom"" backcolor=""none"" bordercolor=""none""
                                            dock=""fill"" size=""400,400"" candragsplitter=""true"" splitmode=""percentsize"" splitterposition=""200,1"">
                                            <div type=""custom"" cid=""mychart"" name=""chart4"" cycle=""15"" nativerefresh=""true"" candledivpercent=""1""
                                                voldivpercent=""0"" />
                                            <div type=""custom"" cid=""mychart"" name=""chart5"" cycle=""20"" nativerefresh=""true"" candledivpercent=""1""
                                                voldivpercent=""0"" />
                                        </div>
                                        <div type=""custom"" cid=""mychart"" name=""chart6"" cycle=""30"" nativerefresh=""true"" candledivpercent=""1""
                                            voldivpercent=""0"" />
                                    </div>
                                    <div type=""splitlayout"" layoutstyle=""toptobottom"" backcolor=""none"" bordercolor=""none""
                                        dock=""fill"" size=""400,400"" candragsplitter=""true"" splitmode=""percentsize"" splitterposition=""267,1"">
                                        <div type=""splitlayout"" layoutstyle=""toptobottom"" backcolor=""none"" bordercolor=""none""
                                            dock=""fill"" size=""400,400"" candragsplitter=""true"" splitmode=""percentsize"" splitterposition=""200,1"">
                                            <div type=""custom"" cid=""mychart"" name=""chart7"" cycle=""1440"" nativerefresh=""true""
                                                candledivpercent=""1"" voldivpercent=""0"" />
                                            <div type=""custom"" cid=""mychart"" name=""chart8"" cycle=""10080"" nativerefresh=""true""
                                                candledivpercent=""1"" voldivpercent=""0"" />
                                        </div>
                                        <div type=""custom"" cid=""mychart"" name=""chart9"" cycle=""43200"" nativerefresh=""true""
                                            candledivpercent=""1"" voldivpercent=""0"" />
                                    </div>
                                </div>
                            </div>
                        </div>
                        <div type=""tabpage"" text=""输入框"" name=""divTextBox"" backcolor=""none"" bordercolor=""none"">
                            <input type=""text"" name=""TextBox"" location=""25,25"" size=""100,20""  />
                            <input type=""text"" name=""TextBox2"" location=""25,65"" size=""100,20""  />
                            <input type=""text"" name=""TextBox3"" location=""25,106"" size=""100,20""  />
                        </div>
                    </div>
                </div>
        </body>
        </html>
        ";
        m_xml.loadXml(xml, null);
        this.PointerPressed += MainView_PointerPressed;
        this.PointerMoved += MainView_PointerMoved;
        this.PointerReleased += MainView_PointerReleased;
        this.PointerWheelChanged += MainView_PointerWheelChanged;
        m_native.update();
        m_native.invalidate();
    }
}
